package com.train.classeval.controller;

import com.train.classeval.entity.Course;
import com.train.classeval.service.ICourseService;
import com.train.classeval.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @program: classeval
 * @author: ChenQi
 * @create: 2021-10-2021/10/22-11-34
 */
@RestController
public class CourseController {

    @Autowired
    private ICourseService courseService;

    /**
     * 添加课程
     * @param course
     * @return
     */
    @PostMapping("/addCourse")
    public JsonData addCourse(@RequestBody Course course) {
        final int i = courseService.addCourse(course);
        if (i != 0) {
            return JsonData.BuildSuccess("添加课程成功");
        }
        return JsonData.BuildSuccess("添加课程失败");
    }

    /**
     * 查找所有课程
     * @return
     */
    @GetMapping("/findAllCourse")
    public JsonData findAllCourse() {
        final List<Course> allCourse = courseService.findAllCourse();
        return JsonData.BuildSuccess(allCourse, "查找成功!");
    }

    /**
     * 查看某个具体的课程
     * @param name
     * @return
     */
    @GetMapping("/findOneCourse/{name}")
    public JsonData findOneCourse(@PathVariable String name) {
        final Course one = courseService.findOne(name);
        if (one != null) {
            return JsonData.BuildSuccess(one, "查询成功!");
        }
        return JsonData.BuildError("查询失败！");
    }

    @DeleteMapping("/deleteCourse/{cid}")
    public JsonData deleteOne(@PathVariable("cid") Integer cid) {
        Integer i = courseService.deleteOne(cid);
        if (i != 0) {
            return JsonData.BuildSuccess("删除成功");
        }
        return JsonData.BuildError("删除失败");
    }
}
